﻿using System;
using System.Globalization;
using EPiServer.Core;

namespace EPiServer.Municipality.Web.UI.PropertyControls {
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    /// <exception cref="NotImplementedException"></exception>
   public class GoogleMapsLocation : IReadOnly<GoogleMapsLocation>, IConvertible {
       
        /* *******************************************************************
        *  Properties 
        * *******************************************************************/
        #region public string Address
        /// <summary>
        /// Get/Sets the Address of the GoogleMapsLocation
        /// </summary>
        /// <value></value>
        public string Address { get; set; }
        #endregion
        #region public double Latitude
        /// <summary>
        /// Get/Sets the Latitude of the GoogleMapsLocation
        /// </summary>
        /// <value></value>
        public double Latitude { get; set; }
        #endregion
        #region public double Longitude
        /// <summary>
        /// Get/Sets the Longitude of the GoogleMapsLocation
        /// </summary>
        /// <value></value>
        public double Longitude { get; set; }
        #endregion
        #region public bool IsReadOnly
        /// <summary>
        /// Gets the IsReadOnly of the GoogleMapsLocation
        /// </summary>
        /// <value></value>
        public bool IsReadOnly {
            get { return _isReadOnly; }
        }
        private bool _isReadOnly;
        #endregion
        #region public static IFormatProvider GoogleMapsDoubleFormatProvider
        /// <summary>
        /// Gets the GoogleMapsDoubleFormatProvider of the GoogleMapsLocation
        /// </summary>
        /// <value></value>
        public static IFormatProvider GoogleMapsDoubleFormatProvider {
            get {
                return CultureInfo.GetCultureInfo("en-US");
            }
        }
        #endregion

        /* *******************************************************************
        *  Methods 
        * *******************************************************************/
        #region public GoogleMapsLocation(string address, double latitude, double longitude)
        /// <summary>
        /// Initializes a new instance of the <b>GoogleMapsLocation</b> class.
        /// </summary>
        /// <param name="address"></param>
        /// <param name="latitude"></param>
        /// <param name="longitude"></param>
        public GoogleMapsLocation(string address, double latitude, double longitude) {
            Address = address;
            Latitude = latitude;
            Longitude = longitude;
        }
        #endregion
        #region public override string ToString()
        /// <summary>
        /// Returns a <see cref="string"/> that represents the current <see cref="RedCross.Municipality.Core.Entities.Google_maps.GoogleMapsLocation"/>.
        /// </summary>
        /// <returns>A <see cref="string"/> that represents the current <see cref="RedCross.Municipality.Core.Entities.Google_maps.GoogleMapsLocation"/>.</returns>
        public override string ToString() {
            return string.Format("{0} ({1},{2})", Address, Latitude.ToString(GoogleMapsDoubleFormatProvider), Longitude.ToString(GoogleMapsDoubleFormatProvider));
        }
        #endregion
        #region public static GoogleMapsLocation Parse(string value)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static GoogleMapsLocation Parse(string value) {
            if (string.IsNullOrEmpty(value))
                return null;

            if (!value.Contains("(") && !value.Contains(")"))
                return null;

            if (value.IndexOf(")") < value.IndexOf("("))
                return null;

            GoogleMapsLocation location = null;

            string address = value.Substring(0, value.IndexOf(" ("));
            string coordinates = value.Substring(value.IndexOf("(") + 1, value.IndexOf(")") - value.IndexOf("(") - 1);
            string[] splitCoordinates = coordinates.Split(new[] { ',' });

            double latitude;
            double longitude;
            if (double.TryParse(splitCoordinates[0], NumberStyles.Float, GoogleMapsDoubleFormatProvider, out latitude)
                && double.TryParse(splitCoordinates[1], NumberStyles.Float, GoogleMapsDoubleFormatProvider, out longitude)) {
                location = new GoogleMapsLocation(address, latitude, longitude);
            }

            return location;
        }
        #endregion
        #region public GoogleMapsLocation CreateWritableClone()
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>A new object that is a copy of this instance.</returns>
        public GoogleMapsLocation CreateWritableClone() {
            GoogleMapsLocation clone = (GoogleMapsLocation)MemberwiseClone();
            clone._isReadOnly = false;
            return clone;
        }
        #endregion      
        #region public void MakeReadOnly()
        /// <summary>
        /// 
        /// </summary>
        public void MakeReadOnly() {
            _isReadOnly = true;
        }
        #endregion

        /* *******************************************************************
        *  IConvertible Members 
        * *******************************************************************/
        #region IConvertible Members

        public TypeCode GetTypeCode() {
            throw new NotImplementedException();
        }

        public bool ToBoolean(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public byte ToByte(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public char ToChar(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public DateTime ToDateTime(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public decimal ToDecimal(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public double ToDouble(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public short ToInt16(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public int ToInt32(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public long ToInt64(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public sbyte ToSByte(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public float ToSingle(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public string ToString(IFormatProvider provider) {
            return ToString();
        }

        public object ToType(Type conversionType, IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public ushort ToUInt16(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public uint ToUInt32(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        public ulong ToUInt64(IFormatProvider provider) {
            throw new NotImplementedException();
        }

        #endregion
    }
}
